#!/bin/sh

# Slackware script to package programs from git for SBo.
# The git SlackBuilds is required for use.
# https://notabug.org/orbea/SlackBuilds-git

# Copyright 2016 orbea
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Dependencies:
# -------------
# awk
# coreutils
# findutils
# diffutils
# git
# grep
# openssh
# patch
# sed
# tar
# xz
#
# Optional:
# ---------
# libnotify
#

REMOTE=${REMOTE:-ovhtoo:~/web/stuff/slackbuilds/src}
SRCURL=${SRCURL:-http://ks392457.kimsufi.com/orbea/stuff/slackbuilds/src}
GITDIR=${GITDIR:-/media/gittings/orbea/SlackBuilds-git}
OUTPUT=${OUTPUT:-/media/data/home/backup/SBo/gittings}
SLKBLD=${SLKBLD:-/media/gittings/orbea/SlackBuilds}
SRCDIR=${SRCDIR:-/media/data/home/src/SBo}
SUBMIT=${SUBMIT:-/tmp/SBo_submit}
SRC=${SRC:-/media/data/home/backup/SBo/src}

set -e

mkdir -p $OUTPUT $SUBMIT $SRC
trap 'rm -rf $OUTPUT' EXIT

arx_info () {
  PRGNAM="$(sed -n 's/^PRGNAM=//p' $arx.info | tr -d '\"')"
  BRANCH="$(sed -n 's/^BRANCH=//p' $arx.info | tr -d '\"')"
  DOWNLOAD="$(sed -n 's/^DOWNLOAD=//p' $arx.info | tr -d '\"' | sed 's/\s.*$//')"
}

arx_dir () {
  PKGDIR=$(basename $(dirname $(dirname $arx)))
  NEWDIR=$GITDIR/$PKGDIR
  SBODIR=$SLKBLD/$PKGDIR
  SBO=$SRCDIR/$PKGDIR
}

arx_version () {
  COMMIT="$(git rev-parse --short HEAD)"
  DATE="$(git show -s --format=%ad --date=format:%Y.%m.%d)"
  VERSION="${DATE}_$COMMIT"
}

arx_submodule () {
  if git config --file .gitmodules --name-only --get-regexp path 2>&1 >/dev/null; then
    git submodule update --init
  fi
}

arx_clone () {
  git clone --depth=1 -b "$BRANCH" "$DOWNLOAD" "$OUTPUT/$PRGNAM"
  cd -- "$OUTPUT/$PRGNAM"
  arx_version
  arx_submodule
  find . -name ".git*" | xargs rm -rf
  cd - 2>&1 >/dev/null
  ( cd $OUTPUT; rename $PRGNAM $PRGNAM-$VERSION $PRGNAM )
}

arx_clean () {
  if [ -f $SRC/$PRGNAM-*.tar.xz ]; then
    mv $SRC/$PRGNAM-*.tar.xz $SRC/.old
    rm -f $SBO/$PRGNAM/$PRGNAM-*.tar.xz
    rm -f $SUBMIT/$PRGNAM-*.tar.gz
  fi
}

arx_var () {
  SED_INFO="s|BRANCH=.*|VERSION=\"$VERSION\"|;s|DOWNLOAD=.*|DOWNLOAD=\"$SRCURL/$PRGNAM-$VERSION.tar.xz\"|"
  SED_MAINTAINER="s|MAINTAINER=.*|MAINTAINER=\"$MAINTAINER\"|"
  SED_BRANCH="s|BRANCH=.*|VERSION=\${VERSION:-$VERSION}|;s|# Copyright.*|# Copyright $(date +"%Y") $MAINTAINER $LOCATION|"
  SED_GIT='s|git clone.*|tar xvf $CWD/$PRGNAM-$VERSION.tar.xz|;s|_git|_SBo|'
  SED_MAKE='s|$PRGNAM-${VERSION}_$BRANCH-$ARCH|$PRGNAM-$VERSION-$ARCH|'
  SED_PRGNAM='s|rm -rf $PRGNAM|rm -rf $PRGNAM-$VERSION|;s|cd $PRGNAM|cd $PRGNAM-$VERSION|'
  SED_BUILD="s|VERSION=.*|VERSION=\${VERSION:-$VERSION}|"
  SED_VERSION="s|VERSION=.*|VERSION=\"$VERSION\"|;s|$PRGNAM-.*.tar.xz\"|$PRGNAM-$VERSION.tar.xz\"|"
  MD5SUM=$(md5sum $SRC/$PRGNAM-$VERSION.tar.xz | cut -d ' ' -f 1)
}

arx_sed () {
  sed -i "$SED_INFO;$SED_MAINTAINER" $SBO/$PRGNAM/$PRGNAM.info
  sed -i '/DOWNLOAD=.*/a MD5SUM=""\nDOWNLOAD_x86_64=""\nMD5SUM_x86_64=""' $SBO/$PRGNAM/$PRGNAM.info
  sed -i "$SED_BRANCH;$SED_GIT;$SED_MAKE;$SED_PRGNAM" $SBO/$PRGNAM/$PRGNAM.SlackBuild
  sed -i '/COMMIT/d;/DATE/d;/HEAD/d' $SBO/$PRGNAM/$PRGNAM.SlackBuild
}

arx_convert () {
  if [ ! -d $SBO/$PRGNAM ]; then
    cp -a $NEWDIR/$PRGNAM $SBO
    arx_sed
    chmod 755 $SBO/$PRGNAM/$PRGNAM.SlackBuild
  fi
}

arx_new () {
  if [ ! -d $SBODIR/$PRGNAM ]; then
    arx_convert
    mkdir -p $SBODIR/$PRGNAM
    DIFF="-uN"
    MSG=$(awk -F "[()]" '{ for (i=2; i<NF; i+=2) print $i }' $SBO/$PRGNAM/slack-desc | head -1)
    GIT="$PKGDIR/$PRGNAM: Added ($MSG)"
  else
    DIFF="-u"
    GIT="$PKGDIR/$PRGNAM: Upgraded to $VERSION"
  fi
}

arx_hack () {
  # hack for x86_64 only scripts
  if grep -q 'DOWNLOAD="UNSUPPORTED"' $SBO/$PRGNAM/$PRGNAM.info; then
    SED_HACK="s|MD5SUM_x86_64=.*|MD5SUM_x86_64=\"$MD5SUM\"|"
  else
    SED_HACK="s|MD5SUM=.*|MD5SUM=\"$MD5SUM\"|"
  fi
}

arx_newline () {
  for file in $(find $SBO/$PRGNAM -type f -exec grep -Iq . {} \; -and -print); do
    if [ -s "$file" ] && [ "$(tail -c1 "$file"; echo x)" != $'\nx' ]; then
      sed -i '$a\' $file
    fi
  done
}

arx_date () {
  copyright="$(grep '# Copyright' $SBO/$PRGNAM/$PRGNAM.SlackBuild)"
  year="$(date +'%Y')"
  prev="$((year-1))"
  case "$copyright" in
    *$year* ) return ;;
    *-$prev* ) new_copy="$(printf %s "$copyright" | sed "s/-${prev}/-${year}/")" ;;
    *$prev* ) new_copy="$(printf %s "$copyright" | sed "s/${prev}/${prev}-${year}/")" ;;
  esac
  sed -i "s|$copyright|$new_copy|" $SBO/$PRGNAM/$PRGNAM.SlackBuild
}

arx_edit () {
  arx_var
  arx_new
  arx_hack
  arx_newline
  arx_date
  sed -i "$SED_BUILD" $SBO/$PRGNAM/$PRGNAM.SlackBuild
  sed -i "$SED_VERSION;$SED_HACK" $SBO/$PRGNAM/$PRGNAM.info
}

arx_commit () {
  ( cd $SBO; diff $DIFF $SBODIR/$PRGNAM $PRGNAM | tee $OUTPUT/$PRGNAM.patch 2>&1 >/dev/null )
  cd $SBODIR/$PRGNAM
  patch -p1 -i $OUTPUT/$PRGNAM.patch
  git add .
  git commit -m "$GIT"
  cd - 2>&1 >/dev/null
}

arx_submit () {
  cp -a $SBODIR/$PRGNAM $SUBMIT
  cd $SUBMIT
  chmod 755 $PRGNAM/$PRGNAM.SlackBuild
  tar cvzf $PRGNAM.tar.gz $PRGNAM 2>&1 >/dev/null
  rm -rf $PRGNAM
  scp $SRC/$PRGNAM-$VERSION.tar.xz $REMOTE
  cd - 2>&1 >/dev/null
}

arx_pkg () {
  if [ ! -f $SRC/$PRGNAM-$VERSION.tar.xz ]; then
    arx_clean
    echo "Compressing $PRGNAM-$VERSION.tar.xz now..."
    cd $OUTPUT; tar cf - $PRGNAM-$VERSION | xz -c9 - > $SRC/$PRGNAM-$VERSION.tar.xz; cd - 2>&1 >/dev/null
    arx_edit
    cp -a $SRC/$PRGNAM-$VERSION.tar.xz $SBO/$PRGNAM
    arx_commit
    arx_submit
  fi
}

arx_name () {
  if [ ! -z $1 ]; then
    ARX="$1"
  else
    echo "Specify the program you want to package with ./arxpkg foo"
    exit 1
  fi
}

arx_all () {
  for arx in $(find $GITDIR -type f -name "$ARX.info" | rev | cut -f2 -d"." | rev); do
    arx_info
    arx_dir
    arx_clone
    arx_pkg
  done
}

arx_notify () {
  if [ -x /usr/bin/notify-send ]; then
    notify-send "$PRGNAM is ready to send to SBo"
  else
    echo "$PRGNAM is ready to send to SBo"
  fi
}

arx_name $1
arx_all
arx_notify
